---
title: 'Supplementary Material - Following the trail of words: mapping trending topics of graduate research in Brazil’s  IR (1987-2018)'
author: 
  - Thales Carvalho (UFMG)
  - Vinicius Santos (UFMG)
  - Yulieth Martínez (UFMG)
date: '2023'
output:
  pdf_document: 
    keep_tex: true
  word_document: default
header-includes:
- \usepackage{caption}
- \usepackage{xcolor}
- \usepackage{float}
- \usepackage{pdflscape}
- \usepackage{afterpage}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(stargazer)
library(dplyr)
library(knitr)
library(kableExtra)
library(ggplot2)
library(here)
library(rdrobust)
library(tidyverse)
library(xtable)
library(stm)
library(papeR)
library(magick)
load("replication_workspace.RData")
options(scipen = 100)
df2 <- df2 %>% rename(year = AnoBase)
```

\newpage

```{r, echo=FALSE, include=FALSE}
lab <- labelTopics(topic2, n= 15, topics = 1:49)
pb <- as.data.frame(lab$prob) %>% unite("prob",1:15, sep=", ")
fx <- as.data.frame(lab$frex) %>% unite("FREX",1:15, sep=", ")
tlabels <- c("N/A", "N/A", "Industry, Technology, and Innovation", "Foreign Policy Decision-Making", "Agricultural Negotiations", "N/A", "Southern Cone States", "Bi and Multilateral Relations", "Refugees", "Strategic Studies", "Environment and Climate Change", "Health and Education Policies", "International Cooperation", "European Studies", "History", "Amazon and Borders", "International Economy", "Multilateral Trade Regimes", "Terrorism", "Gender Studies", "Portuguese Africa", "Inter-American HR System", "National Defense", "Migration Flows", "Peace Operations", "UN Security Council", "Traditional Communities", "Energy Resources", "US Foreign Policy", "South American Integration", "Humanitarian Intervention", "Nuclear Regime", "Intl. Security and Drug Trafficking", "Diplomacy", "International Economic Crises", "Post-Positivism", "Subnational Actors", "Southern Cone Integration", "Asian Powers", "Foreign Investment", "Natl. Perception and Public Opinion", "Political Regime", "International Law", "International Trade", "Non-state Actors", "Geopolitics in the Atlantic", "Brazilian Foreign Policy", "Latin American Integration", "MINUSTAH") %>% as.data.frame()
colnames(tlabels) = c("name")
labls <- cbind(tlabels, pb, fx)
labls$n <- row.names(labls)
labls <- labls %>% select(n, name, prob, FREX) %>% mutate(n = as.numeric(n)) %>%
  rename("Number" = n, "Highest Probabilities" = prob, "Topic Label" = name)

labls <- xtable(labls, type="latex")
```

```{r, results='asis', echo=FALSE}
kb0 <- kbl(labls, caption="Topic Labels", longtable = T) %>% kable_paper(full_width=F, latex_options =c("repeat_header"))%>%
  column_spec(2, width = "7em") %>%
  column_spec(3, width = "18em")%>%
  column_spec(4, width = "18em")
kb0
```

\newpage


```{r, echo=FALSE, include=TRUE, message=FALSE, warning=FALSE, results='asis', caption = 'Summary Statistics'}
df3 <- df2 %>% select(-docnum)
kb1 <- knitr::kable(xtable(summarize(df3, type="numeric", digits=3, digits.pval =3)), caption="Summary Statistics") %>% kable_paper(full_width=T)
kb1
```

\newpage
\landscape

```{r, echo=FALSE, warning=FALSE, message=FALSE, include=TRUE, results='asis'}
fit3 <- lm(Topic3 ~ poly(year, 5), data=df2)
fit4 <- lm(Topic4 ~ poly(year, 5), data=df2)
fit5 <- lm(Topic5 ~ poly(year, 5), data=df2)
fit7 <- lm(Topic7 ~ poly(year, 5), data=df2)
fit8 <- lm(Topic8 ~ poly(year, 5), data=df2)
fit9 <- lm(Topic9 ~ poly(year, 5), data=df2)
fit10 <- lm(Topic10 ~ poly(year, 5), data=df2)
fit11 <- lm(Topic11 ~ poly(year, 5), data=df2)
fit12 <- lm(Topic12 ~ poly(year, 5), data=df2)

stargazer(fit3, fit4, fit5, fit7, fit8, fit9, fit10, fit11, fit12, type = "latex",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (1)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")
```

\newpage

```{r, echo=FALSE, warning=FALSE, message=FALSE, include=TRUE, results='asis'}
fit13 <- lm(Topic13 ~ poly(year, 5), data=df2)
fit14 <- lm(Topic14 ~ poly(year, 5), data=df2)
fit15 <- lm(Topic15 ~ poly(year, 5), data=df2)
fit16 <- lm(Topic16 ~ poly(year, 5), data=df2)
fit17 <- lm(Topic17 ~ poly(year, 5), data=df2)
fit18 <- lm(Topic18~ poly(year, 5), data=df2)
fit19 <- lm(Topic19 ~ poly(year, 5), data=df2)
fit20 <- lm(Topic20 ~ poly(year, 5), data=df2)
fit21 <- lm(Topic21 ~ poly(year, 5), data=df2)
fit22 <- lm(Topic22 ~ poly(year, 5), data=df2)
fit23 <- lm(Topic23 ~ poly(year, 5), data=df2)
fit24 <- lm(Topic24 ~ poly(year, 5), data=df2)

stargazer(fit13, fit14, fit15, fit16, fit17, fit18, fit19, fit20, type = "latex",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (2)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")
```

\newpage
```{r, echo=FALSE, warning=FALSE, message=FALSE, include=TRUE, results='asis'}

fit25 <- lm(Topic25 ~ poly(year, 5), data=df2)
fit26 <- lm(Topic26 ~ poly(year, 5), data=df2)
fit27 <- lm(Topic27 ~ poly(year, 5), data=df2)
fit26 <- lm(Topic26 ~ poly(year, 5), data=df2)
fit27 <- lm(Topic27 ~ poly(year, 5), data=df2)
fit28 <- lm(Topic28 ~ poly(year, 5), data=df2)
fit29 <- lm(Topic29 ~ poly(year, 5), data=df2)
fit30 <- lm(Topic30 ~ poly(year, 5), data=df2)
fit31 <- lm(Topic31 ~ poly(year, 5), data=df2)

stargazer(fit21, fit22, fit23, fit24, fit25, fit26, fit27, fit28,type = "latex",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (3)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")
```

\newpage

```{r, echo=FALSE, warning=FALSE, message=FALSE, include=TRUE, results='asis'}

fit32 <- lm(Topic32 ~ poly(year, 5), data=df2)
fit33 <- lm(Topic33 ~ poly(year, 5), data=df2)
fit34 <- lm(Topic34 ~ poly(year, 5), data=df2)
fit35 <- lm(Topic35 ~ poly(year, 5), data=df2)
fit36 <- lm(Topic36 ~ poly(year, 5), data=df2)


stargazer(fit29, fit30, fit31, fit32, fit33, fit34, fit35, fit36, type = "latex",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (4)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")

```

\newpage 

```{r, echo=FALSE, warning=FALSE, message=FALSE, include=TRUE, results='asis'}

fit37 <- lm(Topic37 ~ poly(year, 5), data=df2)
fit38 <- lm(Topic38~ poly(year, 5), data=df2)
fit39 <- lm(Topic39 ~ poly(year, 5), data=df2)
fit40 <- lm(Topic40 ~ poly(year, 5), data=df2)
fit41 <- lm(Topic41 ~ poly(year, 5), data=df2)
fit42 <- lm(Topic42 ~ poly(year, 5), data=df2)
fit43 <- lm(Topic43 ~ poly(year, 5), data=df2)
fit44 <- lm(Topic44 ~ poly(year, 5), data=df2)
fit45 <- lm(Topic45 ~ poly(year, 5), data=df2)
fit46 <- lm(Topic46 ~ poly(year, 5), data=df2)
fit47 <- lm(Topic47 ~ poly(year, 5), data=df2)
fit48 <- lm(Topic48 ~ poly(year, 5), data=df2)
fit49 <- lm(Topic49 ~ poly(year, 5), data=df2)

stargazer(fit37, fit38, fit39, fit40, fit41, fit42, fit43, fit44, type = "latex",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (5)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")

```

\newpage

```{r, echo=FALSE, warning=FALSE, message=FALSE, include=TRUE, results='asis'}

stargazer(fit45, fit46, fit47, fit48, fit49, type = "latex",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (6)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")

```

```{r, echo=FALSE, warning=FALSE, message=FALSE, include=FALSE, results='asis'}

save_kable(kb0, file = "Table 1 supp.pdf")

save_kable(kb1, file = "Table 2 supp.pdf")

stargazer(fit3, fit4, fit5, fit7, fit8, fit9, fit10, fit11, fit12, type = "html", out = "Table 3 supp.html",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (1)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")

stargazer(fit13, fit14, fit15, fit16, fit17, fit18, fit19, fit20, type = "html", out = "Table 5 supp.html",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (2)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")

stargazer(fit29, fit30, fit31, fit32, fit33, fit34, fit35, fit36, type = "html", out = "Table 6 supp.html",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (4)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")

stargazer(fit37, fit38, fit39, fit40, fit41, fit42, fit43, fit44, type = "html", out = "Table 7 supp.html",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (5)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")

stargazer(fit45, fit46, fit47, fit48, fit49, type = "html", out = "Table 8 supp.html",
                     notes.append = FALSE,
                     model.names=FALSE, single.row=FALSE,
          notes.align = "l", omit.stat=c("ser", "f", "rsq", "adj.rsq"), out.header=FALSE, header=FALSE,
title = 'Polynomial Models (6)', font.size='small', notes = "*p<0.1; **p<0.05; ***p<0.01")
```